package com.aaa.mythread;

import javax.management.ObjectName;

public class MyThread9tongxin {

    private Object obj = new Object();
    private int i=0;



    public  void odd(){
        // i =1
        // 奇数
        while(i<10){
            synchronized (obj){
                if(i%2==1){
                    System.out.println("当前输出的值是奇数，值是:"+i);
                    i++;
                    //
                    obj.notify();
                }else{
                    // 当前的线程让出去
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }


    public  void even(){
        // 奇数
        while(i<10){
            synchronized (obj){
                if(i%2==0){
                    System.out.println("当前输出的值是偶数，值是:"+i);
                    i++;
                    //
                    obj.notify();
                }else{
                    // 奇数  等待
                    try {
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }

    public static void main(String[] args) {

        MyThread9tongxin myThread9tongxin = new MyThread9tongxin();
        // 匿名内部类
      Runnable runnable = new Runnable() {
          @Override
          public void run() {
              myThread9tongxin.odd();
          }
      };

        Runnable runnable2 = new Runnable() {
            @Override
            public void run() {
                myThread9tongxin.even();
            }
        };

        Thread thread = new Thread(runnable);
        Thread thread1 = new Thread(runnable2);
        thread.start();
        thread1.start();
    }
}
